﻿using System;
using System.Collections.Generic;

namespace DataCoreTechnologies.SqlServer
{
   /// <summary>
   /// Contains information about a foreign key belonging to a table.
   /// </summary>
   public sealed class ForeignKey
   {
      #region Public
      //Properties
      /// <summary>
      /// A number that uniquely identifies this class instance.
      /// </summary>
      public Guid InstanceId { get; private set; }
      /// <summary>
      /// The table this foreign key belongs to.
      /// </summary>
      public Table Table { get; internal set; }
      /// <summary>
      /// A titled used to identify this foreign key within the database.
      /// </summary>
      public string Name { get; internal set; }
      /// <summary>
      /// Indicator stating whether this foreign key has been disabled.
      /// </summary>
      public Boolean IsDisabled { get; internal set; }
      /// <summary>
      /// Indicator stating whether this foreign key is trusted.
      /// </summary>
      public Boolean IsNotTrusted { get; internal set; }
      /// <summary>
      /// Indicator stating whether this foreign key was named by the system.
      /// </summary>
      public Boolean IsSystemNamed { get; internal set; }
      /// <summary>
      /// Description of the referential action caused by delete statements.
      /// </summary>
      public string DeleteReferentialActionDescription { get; internal set; }
      /// <summary>
      /// Description of the referential action caused by update statements.
      /// </summary>
      public string UpdateReferentialActionDescription { get; internal set; }
      /// <summary>
      /// Collection of foreign key column pairs associated with this foreign key.
      /// </summary>
      public IEnumerable<ForeignKeyColumns> ForeignKeyColumns
      {
         get
         {
             for (int index = 0; index < _foreignKeyColumns.Count; index++)
             {
                 yield return _foreignKeyColumns[index];
             }
         }
      }
      /// <summary>
      /// Collection of extended properties associated to this foreign key.
      /// </summary>
      public IEnumerable<ExtendedProperty> ExtendedProperties
      {
         get
         {
             for (int index = 0; index < _extendedProperties.Count; index++)
             {
                 yield return _extendedProperties[index];
             }
         }
      }
      #endregion Public
      #region Internal
      //Constructor
      /// <summary>
      /// Creates a foreign key and associates it to the table specified by the parameter.
      /// </summary>
      /// <param name="container">The table this foreign key belongs to.</param>
      internal ForeignKey(Table container)
      {
         Table = container;
         InstanceId = System.Guid.NewGuid();
      }
      //Properties
      /// <summary>
      /// A number used to uniquely identify this foreign key within the database.
      /// </summary>
      internal int Id {get; set; }
      //Methods
      /// <summary>
      /// Associates a foreign key column pair to this foreign key.
      /// </summary>
      /// <param name="item">The columns that are part of this foreign key.</param>
      internal void AddColumns(ForeignKeyColumns item) { _foreignKeyColumns.Add(item); }
      /// <summary>
      /// Associates a extended property to this foreign key.
      /// </summary>
      /// <param name="extendedProperty"></param>
      internal void AddExtendedProperty(ExtendedProperty extendedProperty) { _extendedProperties.Add(extendedProperty); }
      #endregion Internal
      #region Private
      //Properties
      private List<ForeignKeyColumns> _foreignKeyColumns = new List<ForeignKeyColumns>();
      private List<ExtendedProperty> _extendedProperties = new List<ExtendedProperty>();
      //Methods
      #endregion Private
   }
}
